package com.itheima.advantagesup.controller;

import com.itheima.advantagesup.common.R;
import com.itheima.advantagesup.dto.UserDTO;
import com.itheima.advantagesup.service.SysUserService;
import com.itheima.advantagesup.vo.PageUserVO;
import com.itheima.advantagesup.vo.RoleInfoVO;
import com.itheima.advantagesup.vo.UserInfoVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/sys_user")
@Slf4j
public class UserController {
    private final SysUserService sysUserService;

    /**
     * 分页查询
     * @param login_name
     * @param status
     * @param phone
     * @param page
     * @param limit
     * @return {@link R}
     */
    @GetMapping("/list")
    public R<PageUserVO> page(
            String login_name,
            Integer status,
            String phone,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit
                  ){
        PageUserVO pageUserVO = sysUserService.page(login_name,status,phone,page,limit);
        return R.success(pageUserVO);
    }

    /**
     * 添加
     * @param userDTO
     * @return {@link R}
     */
    @PostMapping("/add")
    public R<?> add(@RequestBody UserDTO userDTO){
        sysUserService.add(userDTO);
        return R.success();
    }

    /**
     * 回显
     * @param id
     * @return {@link R}
     */
    @GetMapping("/rowInfo")
    public R<UserInfoVO> rowInfo(String id){
        UserInfoVO userInfoVO = sysUserService.rowInfo(id);
        return R.success(userInfoVO);
    }

    /**
     * 修改
     * @return {@link R}
     */
    @PostMapping("/update")
    public R<?> update(@RequestBody UserDTO userDTO){
        sysUserService.update(userDTO);
        return R.success();
    }

    @GetMapping("/del")
    public R delete(String id){
        sysUserService.delete(id);
        return R.success();
    }

}
